package com.mac.hosp_pre.admin.controller;

import com.mac.hosp_pre.admin.model.resp.DictRespDto;
import com.mac.hosp_pre.admin.model.resp.ExcErrorInfoDto;
import com.mac.hosp_pre.reply.R;
import com.mac.hosp_pre.admin.service.DictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Tag(name = "数据字典接口")
@RestController
@RequestMapping("/admin/hosp/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @Operation(summary = "导入数据字典")
    @PostMapping("importData")
    public R importDict(MultipartFile file) {
        List<ExcErrorInfoDto> excErrorInfoDtos = dictService.importDictData(file);
        return R.success(excErrorInfoDtos);
    }

    @Operation(summary = "导出数据字典接口")
    @GetMapping("exportData")
    public void exportDict(HttpServletResponse response) {
        dictService.exportDictData(response);
    }

    @Operation(summary = "根据dictCode获取下级节点")
    @Parameter(name = "dictCode", description = "节点编码",in = ParameterIn.PATH)
    @GetMapping("findByDictCode/{dictCode}")
    public R findByDictCode(@PathVariable String dictCode) {
        List<DictRespDto> list = dictService.findByDictCode(dictCode);
        return R.success(list);
    }

    @Operation(summary = "根据数据id查询子数据列表")
    @Parameter(name = "id", description = "节点id",in = ParameterIn.PATH)
    @GetMapping("findChildData/{id}")
    public R findChildData(@PathVariable Long id) {
        List<DictRespDto> chlidData = dictService.findChlidData(id);
        return R.success(chlidData);
    }

    @Operation(summary = "根据dictcode和value查询")
    @Parameters(value = {
            @Parameter(name = "dictCode", description = "节点编码",in = ParameterIn.PATH),
            @Parameter(name = "value", description = "节点值",in = ParameterIn.PATH)
    })
    @GetMapping("getName/{dictCode}/{value}")
    public R getName(@PathVariable String dictCode,
                          @PathVariable String value) {
        String dictName = dictService.getDictName(dictCode,value);
        return R.success(dictName);
    }

    //根据value查询
    @Operation(summary = "根据value查询")
    @Parameter(name = "value", description = "节点值",in = ParameterIn.PATH)
    @GetMapping("getName/{value}")
    public R getName(@PathVariable String value) {
        String dictName = dictService.getDictName("",value);
        return R.success(dictName);
    }
}
